package top.csjss.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import top.csjss.hospital.entity.SysMenu;
import top.csjss.hospital.entity.SysRole;
import top.csjss.hospital.entity.SysRoleExample;
import top.csjss.hospital.enums.HospitalExceptionEnums;
import top.csjss.hospital.exception.HospitalException;
import top.csjss.hospital.mapper.SysRoleMapper;
import top.csjss.hospital.service.SysMenuService;
import top.csjss.hospital.service.SysRoleService;
import top.csjss.hospital.util.ExampleUtil;
import top.csjss.hospital.vo.LayUIDTreeVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ztf
 * @Date 17:51 2021/10/18
 **/
@Service
public class SysRoleServiceImpl implements SysRoleService {

    private Logger log = LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查询全部角色信息
     * @return
     */
    @Override
    public List<SysRole> findAll() {
        return sysRoleMapper.selectByExample(null);
    }

    /**
     * 展示学生信息
     * @param rolename
     * @param roledesc
     * @param available
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<SysRole> loadAllRole(String rolename, String roledesc, Integer available, Integer page, Integer limit) {
        //分页
        PageHelper.startPage(page, limit);
        //条件
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(rolename)){
            criteria.andRoleNameLike(ExampleUtil.any(rolename));
        }
        if(!StringUtils.isEmpty(roledesc)){
            criteria.andRoleDescLike(ExampleUtil.any(roledesc));
        }
        if(available != null){
            criteria.andAvailableEqualTo(available);
        }
        //查询
        List<SysRole> list=sysRoleMapper.selectByExample(example);
        PageInfo<SysRole> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<LayUIDTreeVO> initRoleMenuTreeJSON(Integer roleid) {
        //1.查询全部菜单信息
        List<SysMenu> sysMenuList = sysMenuService.findAll();
        //2.查询当前角色所拥有的菜单
        List<Integer> roleMenuId= sysRoleMapper.findMenuIdByRoleId(roleid);
        //封装数据
        List<LayUIDTreeVO> data=new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            LayUIDTreeVO vo=new LayUIDTreeVO();
            BeanUtils.copyProperties(sysMenu, vo);
            vo.setCheckArr(roleMenuId.contains(vo.getId())?"1":"0");
            data.add(vo);
        }
        /**
         * 返回
         */
        return data;
    }


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveRoleMenu(Integer roleid, Integer[] ids) {
        //删除当前选中角色拥有的全部菜单信息
        sysRoleMapper.deleteRoleMenus(roleid);
        //2. 如果ids不为null并且长度大于0，批量添加角色的菜单信息
        if (ids.length>0&&ids!=null){
            int count=sysRoleMapper.saveRoleMenu(roleid,ids);
            if (count!=ids.length){
                log.error("【添加角色菜单信息】添加失败");
                throw new HospitalException(HospitalExceptionEnums.ROLE_SAVE_ROLE_MENU_ERROR);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteRole(Integer roleid) {
        //查询需要更改的角色
        SysRole sysRole=sysRoleMapper.selectByPrimaryKey(roleid);
        sysRole.setAvailable(0);
        //封装
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(roleid)){
            criteria.andRoleIdEqualTo(roleid);
        }
        int count= sysRoleMapper.updateByExample(sysRole,example);
        if (count!=1){
            log.error("删除角色信息失败");
            throw new HospitalException(HospitalExceptionEnums.ROLE_DELETE_ERROR);
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insertRole(String rolename, String roledesc, Integer available) {
        //查询是否存在同名角色
        SysRoleExample example=new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleNameEqualTo(rolename);
        List<SysRole> sysRoleList = sysRoleMapper.selectByExample(example);
        if (sysRoleList.size()!=0){
            log.error("添加角色失败,已存在同名角色");
            throw new HospitalException(HospitalExceptionEnums.ROLE_INSERT_ERROR);
        }
        //封装数据
        SysRole sysRole=new SysRole();
        sysRole.setRoleName(rolename);
        sysRole.setRoleDesc(roledesc);
        sysRole.setAvailable(available);
        int count= sysRoleMapper.insert(sysRole);
        if (count!=1){
            log.error("添加角色失败");
            throw new HospitalException(HospitalExceptionEnums.ROLE_DELETE_ERROR);
        }
    }

    @Override
    public void updateRole(Integer roleid, String rolename, String roledesc, Integer available) {
        //封装数据
        SysRole sysRole=new SysRole();
        sysRole.setRoleId(roleid);
        sysRole.setRoleName(rolename);
        sysRole.setRoleDesc(roledesc);
        sysRole.setAvailable(available);
        int count= sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        if (count!=1){
            log.error("修改角色失败");
            throw new HospitalException(HospitalExceptionEnums.ROLE_UPDATE_ERROR);
        }
    }
}
